package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.FileDestruction;
import com.amsp.oa.oademo.entity.FileDestructionDetail;
import com.amsp.oa.oademo.mapper.FileDestructionMapper;
import com.amsp.oa.oademo.mapper.FileDestructionDetailMapper;
import com.amsp.oa.oademo.service.FileDestructionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class FileDestructionServiceImpl extends ServiceImpl<FileDestructionMapper, FileDestruction>
        implements FileDestructionService {

    @Autowired
    private FileDestructionDetailMapper detailMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdateWithDetails(FileDestruction fileDestruction) {
        // 保存主表
        boolean result = saveOrUpdate(fileDestruction);
        if (result && fileDestruction.getDetails() != null) {
            // 如果是更新，先删除原有明细
            if (fileDestruction.getId() != null) {
                detailMapper.delete(new LambdaQueryWrapper<FileDestructionDetail>()
                        .eq(FileDestructionDetail::getDestructionId, fileDestruction.getId()));
            }

            // 保存新的明细
            for (FileDestructionDetail detail : fileDestruction.getDetails()) {
                detail.setDestructionId(fileDestruction.getId());
                detailMapper.insert(detail);
            }
        }
        return result;
    }

    @Override
    public IPage<FileDestruction> pageWithDetails(Long current, Long size, String keyword) {
        Page<FileDestruction> page = new Page<>(current, size);

        // 查询主表数据
        LambdaQueryWrapper<FileDestruction> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(keyword), FileDestruction::getDestructionReason, keyword)
                .or()
                .like(StringUtils.hasText(keyword), FileDestruction::getHandler, keyword)
                .orderByDesc(FileDestruction::getCreateTime);

        page = page(page, wrapper);

        // 查询明细数据
        for (FileDestruction record : page.getRecords()) {
            List<FileDestructionDetail> details = detailMapper.selectList(
                    new LambdaQueryWrapper<FileDestructionDetail>()
                            .eq(FileDestructionDetail::getDestructionId, record.getId()));
            record.setDetails(details);
        }

        return page;
    }

    @Override
    public FileDestruction getWithDetails(Long id) {
        FileDestruction fileDestruction = getById(id);
        if (fileDestruction != null) {
            List<FileDestructionDetail> details = detailMapper.selectList(
                    new LambdaQueryWrapper<FileDestructionDetail>()
                            .eq(FileDestructionDetail::getDestructionId, id));
            fileDestruction.setDetails(details);
        }
        return fileDestruction;
    }
}